// pages/home/home.ts
import APIZPZ from "../../../utils/apiZpz";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    previewModel: <any>{ lastTime: undefined, local: undefined },//展示机型
    scrollViewHeight: '440rpx',
    firstCategoryList: <any>[],
    activedId: 1,//选中的一级分类名称
    secondCategoryList: <any>[],
    secondActivated: 0,
    thirdCategoryList: <any>[],
    thirdActivated: 0,
    selectedThirdCategoryId: '',// 当前分类id
    pageNum: 1, // 页码
    pageCount: 1, // 总页数
    modelList: <any>[],// 机型列表
    oldTime: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(data: any) {
    this.setData({
      activedId: data.activedId * 1 || 1
    });
    console.log(this.data.activedId)
    APIZPZ.getCategorys().then((res: any) => {
      this.setData({
        firstCategoryList: res.data
      });
      this.data.firstCategoryList.forEach((item: any) => {
        if (this.data.activedId === item.id) {
          this.setData({
            secondCategoryList: item?.child?.length ? item?.child : []
          });
        }
      });
      this.setData({
        thirdCategoryList: this.data.secondCategoryList[this.data.secondActivated]?.['child'] || [],
        selectedThirdCategoryId: this.data.secondCategoryList[this.data.secondActivated]?.['child'][0]['id'] || ''
      });
      this.getModelList();
    });
  },
  // 一级分类改变
  changeOneLevel(e: any) {
    const data = e.currentTarget.dataset;
    this.setData({
      activedId: data.id,
      secondCategoryList: data?.child?.length ? data?.child : [],
    });
    this.secondTabChange({
      currentTarget: {
        dataset: {
          index: 0,
          child: this.data.secondCategoryList[0]?.child || []
        }
      }
    });
  },
  // 二级分类改变
  secondTabChange(e: any) {
    const data = e.currentTarget.dataset;
    this.setData({
      secondActivated: data.index,
      thirdCategoryList: data?.child?.length ? data?.child : []
    });
    this.thirdTabChange({
      currentTarget: {
        dataset: {
          index: 0,
          id: this.data.thirdCategoryList[0]?.id || ''
        }
      }
    });
  },
  // 三级分类改变
  thirdTabChange(e: any) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectedThirdCategoryId: data.id,
      thirdActivated: data.index,
      modelList: [],
      pageNum: 1,
      pageCount: 1,
    });
    if (this.data.selectedThirdCategoryId) {
      this.getModelList();
    }
  },
  // 加载下一页
  loadNextPage() {
    if (this.data.pageNum === this.data.pageCount) {
      return;
    }
    this.data.pageNum += 1;
    this.getModelList();
  },
  getModelList() {
    console.log(this.data.selectedThirdCategoryId)
    APIZPZ.getModelByCatrgoryId(this.data.selectedThirdCategoryId, this.data.pageNum).then((res: any) => {
      console.log(res.data)
      this.setData({
        pageCount: res.data.pages,
        modelList: [...this.data.modelList, ...res.data.list]
      });
    })
  },
  getModelEvaluate(e: any) {
    const data = e.currentTarget.dataset.model;
    const time = new Date().getTime();
    if (time - this.data.oldTime < 1000) {
      return;
    }
    this.data.oldTime = time;
    APIZPZ.getModelEvaluate(data.id).then(() => {
      wx.navigateTo({
        url: `/pages/evaluate/model-evaluate/model-evaluate?id=${data.id}`
      });
    });
  },
  toSubmitOrderMultiple() {
    wx.navigateTo({ url: `/pages/order/submit-order-multiple/submit-order-multiple` });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const systemInfo = wx.getSystemInfoSync();
    const name = systemInfo.model.match(/<(.*)>/)?.[0].replace(/<|>/g, '') || '';
    APIZPZ.getCategoryModel(name).then((res: any) => {
      this.setData({
        ['previewModel.lastTime']: res.data.lastTime,
        ['previewModel.local']: res.data.local
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})